<!--
 * @Author: 郭安东
 * @Date: 2021-03-12 16:51:04
 * @LastEditTime: 2021-04-12 15:02:48
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \test\src\views\function\temperature.vue
-->
<template>
  <div class="temperature">
    <el-card class="box-card">
      <el-link class="userName">{{ userName }}</el-link>
      <el-tag :type="statesType" style="margin-left: 30px">{{ states }}</el-tag>
      <span>
        <el-button class="loginOut-btn" @click="dialog = true"><i class="el-icon-switch-button"></i></el-button>
      </span>
      <span>
        <el-button class="updateBtn" @click="updateDialog = true">修改密码</el-button>
      </span>
    </el-card>
    <el-dialog
      title="确定注销用户吗"
      :visible.sync="dialog"
      width="30%"
      center>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="loginOut()">确 定</el-button>
        <el-button @click="dialog = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="修改密码"
      :visible.sync="updateDialog"
      width="30%"
      center>
      <el-form ref="newForm" :model="newForm" label-width="100px">
        <el-form-item label="原密码" required>
          <el-input v-model="newForm.oldPassword"></el-input>
        </el-form-item>
        <el-form-item label="新密码" required>
          <el-input v-model="newForm.newPassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" required>
          <el-input v-model="newForm.newRePssword" type="password"></el-input>
        </el-form-item>
        <el-form-item class="form-box-btn">
          <el-button type="primary" @click="updatePassword()">确 定</el-button>
          <el-button @click="updateDialog = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <four-menu :buts-data="dataFourMenu" :title=" '用户个人管理' " class="four-menu"/>
    <el-card class="box-card">
      <div class="form-box">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="姓名" required>
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="社区" required>
              <el-select v-model="optionCommunity.value" :placeholder="form.community" style="width: 380px" @change="getEstate(optionCommunity.value)">
                <el-option
                  v-for="item in optionCommunity"
                  :key="item.value"
                  :value="item.value"
                  :label="item.label">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="小区" required>
              <el-select v-model="form.estate" placeholder="请选择" style="width: 380px">
                <el-option
                  v-for="item in optionEstate"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          <el-form-item label="房号" required>
            <el-input v-model="form.houseNumber"></el-input>
          </el-form-item>
          <el-form-item label="身份证" required>
            <el-input v-model="form.idCard"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-radio-group v-model="form.gender">
              <el-radio label="男"></el-radio>
              <el-radio label="女"></el-radio>
              </el-radio-group>
          </el-form-item>
          <el-form-item label="年龄">
            <el-input v-model="form.age"></el-input>
          </el-form-item>
          <el-form-item label="电话号码" required>
            <el-input v-model="form.phone"></el-input>
          </el-form-item>
          <el-form-item class="form-box-btn">
            <el-button type="primary" @click="onSubmit()">保存</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>
<script>
import FourMenu from '../../components/menu/four-menu'
import { userLoginOut, updateBaseInfo, getUserBasicInfo, queryAllCommunity, queryEstateByCommunityId, updateUser } from '../../api/apply-info'
export default {
  name: 'Update',
  components: { FourMenu },
  data () {
    return {
      index: 0,
      userName: '',
      states: '',
      statesType: '',
      dialog: false,
      updateDialog: false,
      form: {
        name: '',
        community: '',
        estate: '',
        houseNumber: '',
        idCard: '',
        gender: '',
        age: '',
        phone: ''
      },
      newForm: {
        oldPassword: '',
        newPassword: '',
        newRePssword: ''
      },
      insertData: {},
      dataFourMenu: [{ name: '返回' }],
      optionCommunity: [],
      optionEstate: []
    }
  },
  mounted () {
    this.getUserName()
    this.getBasicInfo()
    this.getAllCommunity()
  },
  methods: {
    // 获取账户id
    getUserName () {
      this.userName = sessionStorage.getItem('userName')
      this.userId = sessionStorage.getItem('userId')
      this.states = sessionStorage.getItem('states')
      if (this.states === '状态正常' || this.states === '管理员') {
        this.statesType = 'success'
      } else {
        this.statesType = 'danger'
      }
    },
    // 注销用户
    loginOut () {
      userLoginOut().then(res => {
        console.log('loginOut', res)
        this.dialog = false
        this.$message(res.data.message)
        // 将用户名放入sessionStorage
        sessionStorage.setItem('userName', null)
        sessionStorage.setItem('userId', null)
        sessionStorage.setItem('role', null)
        // 将用户放入vuex
        this.$store.dispatch('setUser', null)
        this.$router.push({ path: '/login' })
      })
    },
    // 获取用户基础信息
    getBasicInfo () {
      getUserBasicInfo(this.userId).then(res => {
        console.log(res)
        this.form = res.data.data
        if (this.form.gender === 1) {
          this.form.gender = '男'
        } else if (this.form.gender === 0) {
          this.form.gender = '女'
        }
      })
    },
    // 查询所有社区
    getAllCommunity () {
      queryAllCommunity().then(res => {
        console.log(res)
        for (let i = 0; i < res.data.data.length; i++) {
          // 把id和名字取出来 放进option里
          const label = res.data.data[i].communityName
          const value = res.data.data[i].id
          const option = { label, value }
          this.optionCommunity.push(option)
        }
      })
    },
    // 查询所属小区信息
    getEstate (val) {
      // 查找对应社区id的社区name
      for (let j = 0; j < this.optionCommunity.length; j++) {
        if (this.optionCommunity[j].value === val) {
          this.form.community = this.optionCommunity[j].label
        }
      }
      queryEstateByCommunityId(val).then(res => {
        for (let i = 0; i < res.data.data.length; i++) {
          // 把id和名字取出来 放进option里
          const label = res.data.data[i].estateName
          const value = res.data.data[i].id
          const option = { label, value }
          this.optionEstate.push(option)
        }
        console.log(this.optionEstate)
      })
    },
    // 提交修改基础信息
    onSubmit () {
      this.insertData = this.form
      for (let j = 0; j < this.optionEstate.length; j++) {
        if (this.optionEstate[j].value === this.form.estate) {
          this.form.estate = this.optionEstate[j].label
        }
      }
      if (this.form.gender === '男') {
        this.insertData.gender = 1
      } else if (this.form.gender === '女') {
        this.insertData.gender = 0
      }
      if (this.form.name !== '' && this.form.community !== '' && this.form.estate !== '' && this.form.houseNumber !== '' && this.form.idCard !== '' && this.form.phone !== '') {
        updateBaseInfo(this.insertData).then(res => {
          console.log('updateBaseInfo res', res)
          this.$message(res.data.message)
        })
      }
    },
    // 修改用户密码
    updatePassword () {
      this.newForm.userId = this.userId
      if (this.newForm.oldPasswd !== '' && this.newForm.newPasswd !== '' && this.newForm.newRePasswd !== '') {
        updateUser(this.newForm).then(res => {
          this.$message(res.data.message)
          if (res.data.message === '修改用户密码成功') {
            this.updateDialog = false
          }
        })
      } else {
        this.$message('检查输入')
      }
    }
  }
}
</script>
<style scoped>
.box-card {
    margin-top: 10px;
    margin-left: 10%;
    width: 1200px;
    border-radius: 20px;
    background-color: rgba(255, 255, 255, 0.8);
}
.userName{
  color: #7196b1;
  font-size: 16px;
  font-weight: bolder;
}
.loginOut-btn{
  float: right;
  margin-top: -5px;
  border-radius: 10px;
  color: #7196b1;
  border: none;
  background-color: rgb(240, 234, 234, 0.6);
}
.updateBtn {
  float: right;
  margin-top: -5px;
  margin-right: 10px;
  border-radius: 10px;
  color: #7196b1;
  border: #7196b1 1px solid;
  background-color: rgb(240, 234, 234, 0.6);
}
.form-box {
  width: 500px;
  margin-top: 10px;
  margin-left: 25%;
}
.form-box-btn {
  margin-left: 25%;
}
</style>
<style>
body{
  background-image: url('../../assets/bg3.jpeg');
  background-size: 100% 1000px;
  background-repeat: no-repeat;
}
.el-icon-switch-button {
  color: #e4252d;
  font-size: 17px;
  font-weight: bolder;
}
</style>
